package com.phelon.epdm;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;

import com.phelon.Global.AS400.Factory;

/**
 * @author pdanfy
 */

public class BillOfMaterial {
	public static void main(String[] args) {
		BillOfMaterial testRev = new BillOfMaterial("ASP9802536", "A  ", "      ");
		try {
			testRev.retrieveRecord("7T");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.exit(0);
	}
	private String site;
	private String parentItem;
	private String parentItemRevision;
	private String apcBillOfMaterial;
	private String alternateBomId;
	private String cumulativeFOCostRollUpFactor;
	private String description;
	private String numberOfComponents;
	private String standardBatchQuantity;
	private String userFieldDate1;
	private String userFieldSwitchA;
	private String userFieldText40;
	private ArrayList<BillOfMaterialComponent>	components;

	
	public BillOfMaterial(String site, String parentItem, String parentItemRevision) {
		this.site = site;
		this.parentItem = parentItem;
		this.parentItemRevision = parentItemRevision;
	}


	public boolean retrieveRecord(String toEnv) throws SQLException {
		boolean found = false;
		String fileEnv = " ";
		if (toEnv == "7M") { 
			fileEnv = " ";
		} else {
			fileEnv = toEnv.substring(1);
		}
		StringBuilder sql = new StringBuilder("SELECT * FROM AMFLIB").append(fileEnv)
				.append(".PSTHDRL0 WHERE PITMCS = ? and STIDCS = ? and PITRCS = ?");
		Connection conn = Factory.getConnection();
		PreparedStatement pst = conn.prepareStatement(sql.toString());
		pst.setString(1, parentItem);
		pst.setString(2, site);
		pst.setString(3, parentItemRevision);
		ResultSet rec = pst.executeQuery();
		if (rec.next()) {
			setSite(rec.getString("STIDCS"));
			setParentItem(rec.getString("PITMCS"));
			setParentItemRevision(rec.getString("PITRCS"));
			setAlternateBomId(rec.getString("APCCCS"));
			setApcBillOfMaterial(rec.getString("ALTSCS"));
			setCumulativeFOCostRollUpFactor(rec.getString("FOCRCS"));
			setDescription(rec.getString("PSHDCS"));
			setNumberOfComponents(rec.getString("NSLCCS"));
			setStandardBatchQuantity(rec.getString("SBQTCS"));
			setUserFieldDate1(rec.getString("UUD1CS"));
			setUserFieldSwitchA(rec.getString("UUSACS"));
			setUserFieldText40(rec.getString("UU40CS"));
			found = true;
		}
		rec.close();
		pst.close();
		components = new ArrayList<BillOfMaterialComponent>();
		StringBuilder sql2 = new StringBuilder("SELECT * FROM AMFLIB").append(fileEnv)
		.append(".PSTDTLL0 WHERE PITMCU = ? and STIDCU = ? and PITRCU = ?");
		PreparedStatement pst2 = conn.prepareStatement(sql2.toString());
		pst2.setString(1, parentItem);
		pst2.setString(2, site);
		pst2.setString(3, parentItemRevision);
		ResultSet rec2 = pst2.executeQuery();
		while (rec2.next()) {
			BillOfMaterialComponent nextOne = new BillOfMaterialComponent(parentItem, site, parentItemRevision);
			nextOne.setComponentItem(rec2.getString("CITMCU"));
			nextOne.setComponentItemRevision(rec2.getString("CITRCU"));
			nextOne.setAlternativeBomId(rec2.getString("ALTSCU"));
			nextOne.setComponentConfigurationId(rec2.getString("CFIDCU"));
			nextOne.setComponentLeadTimeAdjustment(rec2.getString("LTCACU"));
			nextOne.setEffectiveFromDate(rec2.getString("EDTFCU"));
			nextOne.setEffectiveToDate(rec2.getString("EDTTCU"));
			nextOne.setFeatureOptionCode(rec2.getString("FOCDCU"));
			nextOne.setFeatureOptionCostRollUpFactor(rec2.getString("FOCFCU"));
			nextOne.setFeatureOptionNumber(rec2.getString("FONOCU"));
			nextOne.setFeatureOptionPlanningFactor(rec2.getString("FOPFCU"));
			nextOne.setOperationWhereFirstUsed(rec2.getString("OPWUCU"));
			nextOne.setQuantityPerUnit(rec2.getString("QPERCU"));
			nextOne.setUserFieldCodeA(rec2.getString("UUCACU"));
			nextOne.setUserFieldDate1(rec2.getString("UUD1CU"));
			nextOne.setUserFieldQuantity1(rec2.getString("UUQ1CU"));
			nextOne.setUserFieldQuantity2(rec2.getString("UUQ2CU"));
			nextOne.setUserFieldQuantity3(rec2.getString("UUQ3CU"));
			nextOne.setUserFieldQuantity4(rec2.getString("UUQ4CU"));
			nextOne.setUserFieldQuantity5(rec2.getString("UUQ5CU"));
			nextOne.setUserFieldQuantity6(rec2.getString("UUQ6CU"));
			nextOne.setUserFieldQuantity7(rec2.getString("UUQ7CU"));
			nextOne.setUserFieldShortAlphaA(rec2.getString("UUGACU"));
			nextOne.setUserFieldShortQuantity1(rec2.getString("UUT1CU"));
			nextOne.setUserFieldText40(rec2.getString("UU40CU"));
			nextOne.setUserSequence1(rec2.getString("USR1CU"));
			nextOne.setUserSequence2(rec2.getString("USR2CU"));
			components.add(nextOne);
		}
		rec2.close();
		pst2.close();

		Factory.freeConnection(conn);
		if (found == true) {
			return true;	
		}
		else {
			return false;			
		}
	}

	public void changeBomSite(String site)  { 
		this.setSite(site);
		for (BillOfMaterialComponent comp : this.getComponents()) {
			comp.setSite(site);
		}
	}

	public void changeBomRevision(String revision)  { 
		this.setParentItemRevision(revision);
		for (BillOfMaterialComponent comp : this.getComponents()) {
			comp.setParentItemRevision(revision);
		}
	}

	public String getSite() {
		return site;
	}


	public void setSite(String site) {
		this.site = site;
	}


	public String getParentItem() {
		return parentItem;
	}


	public void setParentItem(String parentItem) {
		this.parentItem = parentItem;
	}


	public String getParentItemRevision() {
		return parentItemRevision;
	}


	public void setParentItemRevision(String parentItemRevision) {
		this.parentItemRevision = parentItemRevision;
	}


	public String getApcBillOfMaterial() {
		return apcBillOfMaterial;
	}


	public void setApcBillOfMaterial(String apcBillOfMaterial) {
		this.apcBillOfMaterial = apcBillOfMaterial;
	}


	public String getAlternateBomId() {
		return alternateBomId;
	}


	public void setAlternateBomId(String alternateBomId) {
		this.alternateBomId = alternateBomId;
	}


	public String getCumulativeFOCostRollUpFactor() {
		return cumulativeFOCostRollUpFactor;
	}


	public void setCumulativeFOCostRollUpFactor(String cumulativeFOCostRollUpFactor) {
		this.cumulativeFOCostRollUpFactor = cumulativeFOCostRollUpFactor;
	}


	public String getDescription() {
		return description;
	}


	public void setDescription(String description) {
		this.description = description;
	}


	public String getNumberOfComponents() {
		return numberOfComponents;
	}


	public void setNumberOfComponents(String numberOfComponents) {
		this.numberOfComponents = numberOfComponents;
	}


	public String getStandardBatchQuantity() {
		return standardBatchQuantity;
	}


	public void setStandardBatchQuantity(String standardBatchQuantity) {
		this.standardBatchQuantity = standardBatchQuantity;
	}


	public String getUserFieldDate1() {
		return userFieldDate1;
	}


	public void setUserFieldDate1(String userFieldDate1) {
		this.userFieldDate1 = userFieldDate1;
	}


	public String getUserFieldSwitchA() {
		return userFieldSwitchA;
	}


	public void setUserFieldSwitchA(String userFieldSwitchA) {
		this.userFieldSwitchA = userFieldSwitchA;
	}


	public String getUserFieldText40() {
		return userFieldText40;
	}


	public void setUserFieldText40(String userFieldText40) {
		this.userFieldText40 = userFieldText40;
	}


	public ArrayList<BillOfMaterialComponent> getComponents() {
		return components;
	}


	public void setComponents(ArrayList<BillOfMaterialComponent> components) {
		this.components = components;
	}

}